package com.ruiysoft.crm.controller;

import cn.taroco.common.utils.Query;
import cn.taroco.common.web.Response;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruiysoft.crm.model.entity.BizSpecification;
import com.ruiysoft.crm.service.BizSpecificationService;
import io.swagger.annotations.Api;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author fanyl
 */
@Api(tags = "新人宝宝")
@RestController
@RequestMapping("/specification")
public class BizSpecificationController {

    private final BizSpecificationService bizSpecificationService;

    public BizSpecificationController(BizSpecificationService bizSpecificationService) {
        this.bizSpecificationService = bizSpecificationService;
    }

    /**
     * 通过ID查询信息
     *
     * @param id ID
     * @return
     */

    @GetMapping("/get/{id}")
    public BizSpecification get(@PathVariable Integer id) {
        return bizSpecificationService.getById(id);
    }

    /**
     * 分页查询
     *
     * @param params 分页对象
     * @return 分页对象
     */
    @GetMapping("/list/page")
    public IPage dictPage(@RequestParam Map<String, Object> params) {
        return bizSpecificationService.page(new Query<>(params), new QueryWrapper<>());
    }

    /**
     * 添加
     *
     * @param bizSpecification
     * @return success、false
     */
    @PostMapping("/insert")
    public Response add(@RequestBody BizSpecification bizSpecification) {
        if (StringUtils.hasText(bizSpecification.getCatalogId())) {
            Map<String, Object> sysDict = bizSpecificationService.getSysDictByValue(bizSpecification.getCatalogId(), "specification");
            if (!CollectionUtils.isEmpty(sysDict)) {
                bizSpecification.setCatalogName((String) sysDict.get("label"));
            }
        }
        bizSpecification.setCreateDate(LocalDateTime.now());
        return Response.success(bizSpecificationService.save(bizSpecification));
    }

    /**
     * 删除
     *
     * @param id ID
     * @return R
     */
    @DeleteMapping("/delete/{id}")
    public Response delete(@PathVariable Integer id) {
        return Response.success(bizSpecificationService.removeById(id));
    }

    /**
     * 修改
     *
     * @param bizSpecification
     * @return success/false
     */
    @PutMapping("/update")
    public Response editDict(@RequestBody BizSpecification bizSpecification) {
        bizSpecification.setModificationDate(LocalDateTime.now());
        return Response.success(bizSpecificationService.updateById(bizSpecification));
    }
}
